package com.anlu.base.model;


import com.anlu.base.model.errorcode.IErrCode;
import lombok.Data;

@Data
public class ResultBean<T> {
    /**
     * 状态
     */
    public String resultCode;
    /**
     * 数据
     */
    public T bean;



    /**
     * 扩展数据
     */
    public String extInfo;

    public Throwable e;

    public ResultBean() {

    }


    public Throwable getE() {
        return e;
    }

    public void setE(Throwable e) {
        this.e = e;
    }

    public ResultBean(String resultCode, T bean) {
        this.resultCode = resultCode;
        this.bean = bean;
    }

    public ResultBean(String resultCode, T bean, String extInfo) {
        this.resultCode = resultCode;
        this.bean = bean;
        this.extInfo = extInfo;
    }


    public static <T> ResultBean<T> resultCode(IErrCode errCode) {
        return new ResultBean<>(errCode.getErrCode(), null);
    }

    public static <T> ResultBean<T> resultCode(String resultCode) {
        return new ResultBean<>(resultCode, null);
    }

    public static <T> ResultBean<T> error() {
        return new ResultBean<>(ErrCodeConts.UN_KNOW, null);
    }

    public static <T> ResultBean<T> error(T bean) {
        return new ResultBean<>(ErrCodeConts.UN_KNOW, bean);
    }

    public static <T> ResultBean<T> success() {
        return new ResultBean<>(ErrCodeConts.OK, null);
    }

    public static <T> ResultBean<T> success(T bean) {
        return new ResultBean<>(ErrCodeConts.OK, bean);
    }

    public static <T> ResultBean<T> success(T bean, String extInfo) {
        return new ResultBean<>(ErrCodeConts.OK, bean, extInfo);
    }

    public ResultBean<T> bean(T bean) {
        this.setBean(bean);
        return this;
    }

    public ResultBean<T> e(Throwable e) {
        this.setE(e);
        return this;
    }

    public boolean isSuccess() {
        return null != resultCode && ErrCodeConts.OK.equals(resultCode);
    }


    public T getBean() {
        return bean;
    }

    public void setBean(T bean) {
        this.bean = bean;
    }
}
